<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="./css/style.css">
    <!--引入Vue-->
    <script src="./js/axios.min.js"></script>
    <script src="./js/main.js"></script>
    <script src="../js/vue.js"></script>
    <title>添加</title>
    <style>
        .feng1 {
            background-image: url(./img/fan.png);
            background-size: 100%;
            min-width: calc(20vh / 1872 * 1310);
            width: calc(20vh / 1872 * 1310);
            height: 20vh;
            position: relative;
            color: #000;
        }

        .fan1 {
            background-image: url(./img/fan-2.png);
            width: calc(20vh / 1872 * 942);
            height: calc(20vh / 1872 * 927);
            background-size: 100%;

            position: absolute;
            top: calc(20vh / 1872 * 202)
                /*226px*/
            ;
            left: calc(20vh / 1872 * 147)
                /*26px*/
            ;
            transform-origin: 52.52% 49.087%;
            animation: rotation 0.01s linear infinite;
        }

        .Open1 {
            background-image: url(./img/Open.png);
            width: calc(20vh / 1872 * 80)
                /*80px*/
            ;
            height: calc(20vh / 1872 * 80)
                /*80px*/
            ;
            background-size: 100%;

            position: absolute;
            top: calc(20vh / 1872 * 1419)
                /*50px*/
            ;
            left: calc(20vh / 1872 * 617)
                /*340px*/
            ;
            transform-origin: center;
            animation: rotation 0.01s linear infinit
        }

        .Open1:hover {
            background-image: url(./img/Open-2.png);
        }

        .feng2 {
            background-image: url(./img/feng.png);
            background-size: 100%;
            min-width: calc(20vh / 781.971947 *500);
            width: calc(20vh / 781.971947 *500);
            height: 20vh;
            /* 500 / 781.971947*/
            position: relative;
        }

        .fan2 {
            background-image: url(./img/feng-1.png);
            width: calc(20vh / 781.971947 * 200);
            height: calc(20vh / 781.971947 * 205);
            background-size: 100%;

            position: absolute;
            top: calc(20vh / 781.971947 * 226);
            left: calc(20vh / 781.971947 * 26);
            transform-origin: 47.52% 49.087%;
            animation: rotation 0.01s linear infinite;
        }

        .Open2 {
            background-image: url(./img/Open.png);
            width: calc(20vh / 781.971947 * 80)
                /*80px*/
            ;
            height: calc(20vh / 781.971947 * 80);
            background-size: 100%;

            position: absolute;
            top: calc(20vh / 781.971947 * 50);
            left: calc(20vh / 781.971947 * 340);
            transform-origin: center;
            animation: rotation 0.01s linear infinit
        }

        .Open2:hover {
            background-image: url(./img/Open-2.png);
        }


        .page {
            margin: 0 auto;
            height: calc(100vh - 110px);
            width: 50%;
            background-color: white;
            padding: 20px 60px;
        }
        .page>label,.page>input,.page>select{
            font-size: 20px;
            height: 30px;
            line-height: 60px;
        }
        .page>button{
            padding: 5px 20px;
            font-size: 20px;
            font-weight: 600;
            margin: 10px;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="nav flex-container-between">
            <div>
                <ul class=" flex-container-start">
                    <li><a href="./home.html">主页</a></li>
                    <li><a href="./recharge.html">充值</a></li>
                    <li><a href="./increase.html">添加</a></li>
                </ul>
            </div>
            <div>
                <ul class=" flex-container-end">
                    <li><a @click="exit()">退出登录</a></li>
                </ul>
            </div>
        </div>
        <div class="page">
            <h1>购买家电</h1>
            <label for="address">类型：</label><select name="address" id="address" v-model="fanInfo.type_id">
                <option value="">请选择家电类型</option>
                <option value="1">普通风扇</option>
                <option value="2">斯巴达牌风扇</option>
                <option value="" disabled>其他</option>
            </select>
            <br>
            <div style="position: relative;">
                <div v-bind:class="'fan'+fanInfo.type_id"></div>
                <div v-bind:class="'feng'+fanInfo.type_id"></div>
                <span v-bind:class="'Open'+fanInfo.type_id"></span>
            </div>
            <br>
            <label for="name">为你的电器命个名：</label><input id="name" name="name" type="text" v-model="fanInfo.name" />
            <br><br>
            <button @click="ins">购买</button>
        </div>
    </div>

    <script>
        var vm = new Vue({
            el: '#app',
            data: {
                fanInfo: {
                    type_id: '',
                    name: '',
                }
            },
            methods: {
                ins() {
                    if(this.type_id=='')
                    {
                        alert('请选择类型');
                        return;
                    }
                    axios.post('/appliance/add', {
                            type_id: this.fanInfo.type_id,
                            name: this.fanInfo.name
                        })
                        .then((response) => {
                            const responsedata = response.data
                            console.log(response)
                            // 服务器状态码
                            if (responsedata.code != 0) {
                                // 如果不为0，说明有错误, msg为云端错误信息
                                alert(responsedata.msg);
                                // 截断后面的运行
                                if (responsedata.code == 401)
                                    window.location.href = "./login.html";
                                return;
                            }
                            window.location.href = "./home.html";
                        })
                        .catch(function (error) {
                            console.log(error);
                        });
                },
                exit() {
                    axios.post('/user/logout')
                        .then((response) => {
                            const responsedata = response.data
                            // 服务器状态码
                            if (responsedata.code != 0) {
                                // 如果不为0，说明有错误, msg为云端错误信息
                                alert(responsedata.msg);
                                // 截断后面的运行
                                return;
                            }
                            window.location.href = "./login.html";
                        })
                        .catch(function (error) {
                            console.log(error);
                        });
                }
            }
        })
    </script>
</body>

</html>